package com.hp.util;



import com.hp.entity.*;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

/**

 * @className：MybtisSqlUtil.java

 * @Title:     MybtisSqlUtil

 * @Description: TODO(用一句话描述本文件)

 */
public class MybtisSqlUtil {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
//        String sql = "select distinct TABLE_NAME from information_schema.`COLUMNS` where TABLE_SCHEMA = 'hisystem'";
//        DBHelper dbHelper = DBHelper.getMyHepler();
//        String table_schema = "hisystem";
//        List<Map> table = dbHelper.select(sql);
//        System.out.println(table);
//        for (Map tables : table) {
//            System.out.println(tables);
//        }
        //Role对应实体类
       creatMapperSql(MedicalExamination.class);
    }

    /**
     * 根据实体类生成对应的mybatis  mapper中的sql 语句
     *
     * 目前只能打印在控制台
     *
     * */
    private static void creatMapperSql(Class class1) throws ClassNotFoundException,
            NoSuchFieldException, SecurityException, InstantiationException,
            IllegalAccessException, NoSuchMethodException,
            IllegalArgumentException, InvocationTargetException {

        Object obj = class1.newInstance();
        String pojoname=class1.getName();
        String pojourl=pojoname;
        pojoname=pojoname.substring(pojoname.lastIndexOf(".")+1, pojoname.length());

        Field[] fields = class1.getDeclaredFields();
        StringBuffer reqMap = new StringBuffer("\n<resultMap id=\""+"Map"+pojoname+"\" type=\""+pojourl+"\">");
        for (Field field : fields) {
            String name = field.getName();
            reqMap.append("\n<result column=\""+name+"\" property=\""+name+"\"/>");
        }
        reqMap.append("\n</resultMap>");
        //动态拼接mybtis中查询sql语句
        StringBuffer selSql= new StringBuffer("\n<select id=\""+"findAll"+pojoname+"\" parameterType=\""+pojourl+"\" resultType=\""+pojourl+"\">");
        selSql.append("\nselect");
        for (Field field : fields) {
           String name = field.getName();
            selSql.append("\n"+name+",");
        }
        selSql.append("\nfrom "+pojoname);
        selSql.append("\n<where>");


        //动态拼接mybtis中修改sql语句
        StringBuffer updSql=new StringBuffer();
        updSql.append("\n<update id=\""+"update"+pojoname+"\" parameterType=\""+pojourl+"\">");
        updSql.append("\nupdate "+pojoname);
        updSql.append("\n<trim prefix=\"set\" suffixOverrides=\",\">");


        //生成mybatis中 批量删除sql 语句
        StringBuffer delSql=new StringBuffer();
        delSql.append("\n<delete id=\""+"delete"+pojoname+"\" parameterType=\"java.lang.String\">");
        delSql.append("\ndelete from "+pojoname+"");
        delSql.append("\nwhere id in ");
        delSql.append("\n<foreach collection=\"array\" item=\"item\" open=\"(\" separator=\",\" close=\")\">");
        delSql.append("\n#{item}");
        delSql.append("\n</foreach>");


        delSql.append("\n</delete>");


        //动态拼接mybtis中新增sql语句
        StringBuffer insSql=new StringBuffer();

        insSql.append("\n<insert id=\""+"add"+pojoname+"\" parameterType=\""+pojourl+"\">");
        insSql.append("\ninsert into "+pojoname+"(");
        insSql.append("\n<trim suffixOverrides=\",\">");
        insSql.append("");

        for (Field field : fields) {
            String name = field.getName();
            selSql.append("\n<if test=\""+name+" !=null and "+name+" !='' \"> and "+name+"=#{"+name+"}</if>");
            if(name!="id"){
                updSql.append("\n<if test=\""+name+" !=null and "+name+" !='' \">"+name+"=#{"+name+"} ,</if>");
                insSql.append("\n<if test=\""+name+" !=null and "+name+" !='' \">"+name+",</if>");
            }

        }
        insSql.append("\n</trim>");
        insSql.append("\n)value(");
        insSql.append("\n<trim suffixOverrides=\",\">");
        for (Field field : fields) {
            String name = field.getName();
            if(name!="id"){
                insSql.append("\n<if test=\""+name+" !=null and "+name+" !='' \">#{"+name+"} ,</if>");
            }



        }
        insSql.append("\n</trim>");
        insSql.append("\n)");
        insSql.append("\n</insert>");
        selSql.append("\n</where>");
        selSql.append("\n</select>");
        updSql.append("\n</trim>");
        updSql.append("\nwhere id=#{id}");
        updSql.append("\n</update>");
        String path = DBHelper.class.getResource("").getPath();

        System.out.println(reqMap.toString());
        System.out.println(selSql.toString());
        System.out.println(delSql.toString());
        System.out.println(updSql.toString());
        System.out.println(insSql.toString());
        StringBuffer Dao=new StringBuffer();

        Dao.append("\nList<"+pojoname+"> findAll"+pojoname+"("+pojoname+" entity);");

        Dao.append("\nint add"+pojoname+"("+pojoname+" entity);");

        Dao.append("\nint delete"+pojoname+"(String[] entity);");

        Dao.append("\nint update"+pojoname+"("+pojoname+" entity);");

        System.out.println(Dao.toString());
    }
}
